package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type DictionaryDetailEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDictionaryDetailEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DictionaryDetailEditLogic {
	return &DictionaryDetailEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// DictionaryDetailEdit 更新字典附表
func (l *DictionaryDetailEditLogic) DictionaryDetailEdit(in *property_service.DictionaryDetail) (*property_service.Response, error) {
	// 获取字典附表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).DictionaryDetail
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、CreatedAt、UpdatedAt
	if _, err := do.UpdateColumns(&model.DictionaryDetail{
		DictKey:       in.DictKey,       // 字段唯一key
		Code:          in.Code,          // 字典项编码
		ItemKey:       in.ItemKey,       // 项键
		ItemValue:     in.ItemValue,     // 项值
		Description:   in.Description,   // 项描述
		SortOrder:     in.SortOrder,     // 排序序号
		UpdatedAtuser: in.UpdatedAtuser, // 修改人ID
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
